﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser
{
    class pv_lists_raw
    {
        public List<pv_list_raw> pv_list = new List<pv_list_raw>();
        public int game_type = 1;
        public void load(string s)
        {
            var lists = pv_list;
            using (StreamReader file = new StreamReader(s))
            {
                while (file.Peek() >= 0)
                {
                    string line = file.ReadLine();
                    if (!line.StartsWith("#"))
                    {
                        if (!line.Contains("data_list"))
                        {
                            string[] line2 = line.Split('.');
                            int index = int.Parse(line2[1]);
                            var curPv = lists.Where(c => c.index == index).FirstOrDefault();
                            if (curPv == null)
                            {
                                lists.Add(new pv_list_raw(index));
                                curPv = lists.Where(c => c.index == index).FirstOrDefault();
                            }
                            int coutn = line2[0].Count() + 1 + line2[1].Count() + 1;
                            curPv.line.Add(line.Remove(0, coutn));
                        }
                    }
                }
            }
        }
        
        public void SaveTo(string file)
        {
            StreamWriter outp = new StreamWriter(file);

            var ordered = pv_list.OrderBy(c => c.index.ToString());

            for (int i = 0; i < 236; i++)
                outp.WriteLine("#---------------------------------------------");

            foreach (var i in ordered)
            {
                foreach (var u in i.line)
                {
                    var header = "pv_list." + i.index + ".";
                    outp.WriteLine(header + u);
                }
            }
            outp.WriteLine("pv_list.data_list.length=" + pv_list.Count);
            outp.Close();
        }
    }
}
